package com.liu.domain.service.impl.search_impl;

import com.google.common.collect.Lists;
import com.liu.domain.base.PageVo;
import com.liu.domain.dao.SearchMappingModelDao;
import com.liu.domain.entity.search.SearchMappingModel;
import com.liu.domain.service.search.SearchMappingModelService;
import com.liu.domain.vo.search.SearchRequestVO;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.PageRequest;
import org.springframework.data.domain.Pageable;
import org.springframework.data.domain.Sort;
import org.springframework.data.elasticsearch.core.ElasticsearchTemplate;
import org.springframework.data.elasticsearch.core.query.NativeSearchQueryBuilder;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import java.util.List;
import static org.elasticsearch.index.query.QueryBuilders.multiMatchQuery;
import static org.elasticsearch.index.query.QueryBuilders.termsQuery;

/**
 * @author liuxinghong
 * @Description:
 * @date 2019/8/3/0038:56
 */
@Service
@Transactional
public class SearchMappingModelServiceImpl implements SearchMappingModelService {

    @Autowired
    private ElasticsearchTemplate elasticsearchTemplate;
    @Autowired
    private SearchMappingModelDao searchMappingModelDao;

    @Override
    public  List<SearchMappingModel> save(List<SearchMappingModel> list) {
        Iterable<SearchMappingModel> save = searchMappingModelDao.save(list);
        if (save.iterator().hasNext()){
           return Lists.newArrayList(save);
        }
        return null;
    }

    @Override
    public List<SearchMappingModel> searchPrecise(PageVo<SearchRequestVO> vo) {
        /**
         * 分页
         */
        Pageable pageable;
        if (StringUtils.isNotBlank(vo.getSortField()) && 0 == vo.getDirection()){//倒序
            pageable =  new PageRequest(vo.getPageNO()-1, vo.getPageSize(), Sort.Direction.DESC,vo.getSortField());
        }else if (StringUtils.isNotBlank(vo.getSortField()) && 1 == vo.getDirection()){ //正序
            pageable =  new PageRequest(vo.getPageNO()-1, vo.getPageSize(), Sort.Direction.ASC,vo.getSortField());
        }
        else  {//不排序
            pageable = new PageRequest(vo.getPageNO()-1, vo.getPageSize());
        }
        NativeSearchQueryBuilder queryBuilder = new NativeSearchQueryBuilder();
        if (null != vo){
            if (StringUtils.isNotBlank(vo.getParams().getFields())){
                queryBuilder.withQuery(termsQuery(vo.getParams().getFields(),vo.getParams().getTitle()));
            }
        }
        queryBuilder.withPageable(pageable);
        return elasticsearchTemplate.queryForList(queryBuilder.build(), SearchMappingModel.class);
    }

    @Override
    public List<SearchMappingModel> searchMatch(PageVo<SearchRequestVO> vo) {
        /**
         * 分页
         */
        Pageable pageable;
        if (StringUtils.isNotBlank(vo.getSortField()) && 0 == vo.getDirection()){//倒序
            pageable =  new PageRequest(vo.getPageNO()-1, vo.getPageSize(), Sort.Direction.DESC,vo.getSortField());
        }else if (StringUtils.isNotBlank(vo.getSortField()) && 1 == vo.getDirection()){ //正序
            pageable =  new PageRequest(vo.getPageNO()-1, vo.getPageSize(), Sort.Direction.ASC,vo.getSortField());
        }
        else  {//不排序
             pageable = new PageRequest(vo.getPageNO()-1, vo.getPageSize());
        }
        NativeSearchQueryBuilder queryBuilder = new NativeSearchQueryBuilder();
        if (null != vo){
            if (StringUtils.isNotBlank(vo.getParams().getFields())){
                queryBuilder.withQuery(multiMatchQuery(vo.getParams().getTitle(),vo.getParams().getFields()));
            }
        }
        queryBuilder.withPageable(pageable);
        return elasticsearchTemplate.queryForList(queryBuilder.build(), SearchMappingModel.class);
    }

    @Override
    public List<SearchMappingModel> precise(SearchRequestVO vo) {
        NativeSearchQueryBuilder queryBuilder = new NativeSearchQueryBuilder();
        if (null != vo){
            if (StringUtils.isNotBlank(vo.getFields())){
                queryBuilder.withQuery(termsQuery(vo.getFields(),vo.getTitle())).build();
                return elasticsearchTemplate.queryForList(queryBuilder.build(), SearchMappingModel.class);
            }
        }
        return null;
    }

    @Override
    public List<SearchMappingModel> match(SearchRequestVO vo) {
        NativeSearchQueryBuilder queryBuilder = new NativeSearchQueryBuilder();
        if (null != vo){
            if (StringUtils.isNotBlank(vo.getFields())){
                queryBuilder.withQuery(multiMatchQuery(vo.getTitle(),vo.getFields()));
                return elasticsearchTemplate.queryForList(queryBuilder.build(), SearchMappingModel.class);
            }
        }
        return null;
    }

    @Override
    public SearchMappingModel detail(String id) {
       return searchMappingModelDao.findOne(id);
    }

    @Override
    public void delete(String id) {
         searchMappingModelDao.delete(id);
    }


}
